package cn.danger.com.model.action;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.danger.com.model.entity.Memo;
import cn.danger.com.model.listener.OnCommonListener;
import cn.danger.com.util.Check;
import cn.danger.com.util.Constants;
import cn.danger.com.util.MyNetManager;

/**
 * Created by 俊 on 2016/12/31.
 */
public class MemoModelImpl implements MemoModel {

    private Context context;

    public MemoModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public String check(Memo memo) {
        if (TextUtils.isEmpty(memo.getTitle())) {
            return Constants.TITLE_IS_EMPTY;
        }
        if (TextUtils.isEmpty(memo.getContent())) {
            return Constants.CONTENT_IS_EMPTY;
        }
        return Constants.IS_RIGHT;
    }

    @Override
    public void addMemoToNetwork(Memo memo, final OnCommonListener listener) {
        String check_result = check(memo);
        if (check_result.equals(Constants.IS_RIGHT)) {
            if (new MyNetManager(context).isNetworkAvailable()) {
                memo.save(new SaveListener<String>() {
                    @Override
                    public void done(String objectId, BmobException e) {
                        if (e == null) {
                            listener.onSuccess(objectId);
                        } else {
                            e.printStackTrace();
                            if (Check.checkByCode(e.getErrorCode()) == null) {
                                listener.onError(Constants.FAIL_TO_ADD_MEMO);
                            } else {
                                listener.onError(Check.checkByCode(e.getErrorCode()));
                            }
                        }
                    }
                });
            } else {
                listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
            }
        } else {
            listener.onError(check_result);
        }
    }

    @Override
    public void deleteMemoToNetWork(String objectId, final OnCommonListener listener) {
        Memo memo = new Memo();
        memo.setObjectId(objectId);
        if (new MyNetManager(context).isNetworkAvailable()) {
            memo.delete(new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        listener.onSuccess(null);
                    } else {
                        e.printStackTrace();
                        if (Check.checkByCode(e.getErrorCode()) == null) {
                            listener.onError(Constants.FAIL_TO_DELETE_MEMO);
                        } else {
                            listener.onError(Check.checkByCode(e.getErrorCode()));
                        }
                    }
                }
            });
        } else {
            listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
        }
    }

    @Override
    public void deleteMemosToNetWork(List<BmobObject> memos, final OnCommonListener listener) {
        if (new MyNetManager(context).isNetworkAvailable()) {
            new BmobBatch().deleteBatch(memos).doBatch(new QueryListListener<BatchResult>() {
                @Override
                public void done(List<BatchResult> list, BmobException e) {
                    if (e == null) {
                        for (int i = 0; i < list.size(); i++) {
                            BatchResult result = list.get(i);
                            BmobException ex = result.getError();
                            if (ex == null) {
                                Log.i("danger", "第" + i + "个数据批量删除成功");
                            } else {
                                Log.i("danger", "第" + i + "个数据批量删除失败：" + ex.getMessage() + "," + ex.getErrorCode());
                            }
                        }
                        listener.onSuccess(Constants.SUCCESS_TO_DELETE_MEMO);
                    } else {
                        listener.onError(Constants.FAIL_TO_DELETE_MEMO);
                    }
                }
            });
        } else {
            listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
        }
    }

    @Override
    public void updateMemoToNetWork(Memo memo, final OnCommonListener listener) {
        String check_result = check(memo);
        if (check_result.equals(Constants.IS_RIGHT)) {
            if (new MyNetManager(context).isNetworkAvailable()) {
                memo.update(memo.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            listener.onSuccess(null);
                        } else {
                            e.printStackTrace();
                            if (Check.checkByCode(e.getErrorCode()) == null) {
                                listener.onError(Constants.FAIL_TO_UPDATE_MEMO);
                            } else {
                                listener.onError(Check.checkByCode(e.getErrorCode()));
                            }
                        }
                    }
                });
            } else {
                listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
            }
        } else {
            listener.onError(check_result);
        }
    }
}
